﻿using System;
using System.Linq;
using Telerik.Web.UI;
using Thuoc24h.Business;
using Thuoc24h.Common;
using Thuoc24h.Data;

namespace Thuoc24h.AdminCP.Pages
{
    public partial class AdsEdit : BasePage.BasePages
    {
        private int _id;
        private Ad _obj = new Ad();

        protected void Page_Load(object sender, EventArgs e)
        {
            _id = Convert.ToInt32(Request.QueryString["ID"]);
            if (!Page.IsPostBack)
            {
                pnlEmbed.Visible = false;
               
                if (_id > 0)
                {
                    btnSave.Text = ActionNames.Update;
                    BindData();
                }
                else
                {
                    btnSave.Text = ActionNames.AddNew;
                }
                }

        }

        private void GetData()
        {
            _obj.Link = txtLink.Text;
            _obj.ImageUrl = txtFileName.Text;
            _obj.PositionId = cbbPosition.SelectedValue.ToInt32Return0();
            _obj.EmbedCode = txtEmbedCode.Text;
            _obj.SortOrder = radNumberOrd.Text.ToInt32Return0();
        }
        private void Reset()
        {
            txtLink.Text = string.Empty;
            txtFileName.Text = string.Empty;
            txtEmbedCode.Text = string.Empty;
            cbbPosition.SelectedIndex = 0;
            radNumberOrd.Text = "1";
        }
        private void SaveData()
        {
            if (_id > 0)
            {
                _obj = context.Ads.SingleOrDefault(item => item.Id == _id);
                if (_obj != null)
                {
                    GetData();
                }

            }
            else
            {
                GetData();
                context.Ads.AddObject(_obj);
            }

            if (context.SaveChanges() > 0)
            {

                Reset();
                Response.Redirect(UrlManager.Admin.AdsView);
            }
        }
        private void BindData()
        {
            _obj = ReviewRepository.AdsById(_id);
            txtLink.Text = _obj.Link;
            if (_obj.ImageUrl != string.Empty)
            {
                pnlImage.Visible = true;
                txtEmbedCode.Visible = false;
                ccAdsype.SelectedValue = "1";
                 txtFileName.Text = _obj.ImageUrl;
            }
            if (_obj.EmbedCode!=string.Empty)
            {
                pnlEmbed.Visible = true;
                pnlImage.Visible = false;
                ccAdsype.SelectedValue = "2";
                txtEmbedCode.Text = _obj.EmbedCode;
                
            }
           
            cbbPosition.SelectedValue = _obj.PositionId.ToString();
            radNumberOrd.Text = _obj.SortOrder.ToString();
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            SaveData();

        }

        protected void OnClick(object sender, EventArgs e)
        {
            Reset();
        }


        protected void ccAdsype_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
          if (ccAdsype.SelectedValue.Equals("1"))
          {
              pnlImage.Visible = true;
              pnlEmbed.Visible = false;
          }
          else
          {
              pnlImage.Visible = false;
              pnlEmbed.Visible = true;
          }
        }
    }
}